import { registerEntityType, requestModel } from '../base'
import { registerEntityTable } from '../table'
import type { Airport } from './types'
import { initMap } from './map'
import { initAirportIndex } from './geo-index'
import { initAirportMapItems } from './map-item'
import { lat2str, lng2str } from '@/geom/great-circle'

export type { Airport } from './types'
export * from './feature'
export { airportMapItem } from './map-item'

function setupAirportTable() {
  registerEntityTable<Airport>({
    type: 'Airport',
    columns: [
      {
        name: 'id',
        cell: (e) => e.id,
        width: 100
      },
      {
        name: '经度',
        cell: (e) => lng2str(e.location.lng),
        width: 180
      },
      {
        name: '纬度',
        cell: (e) => lat2str(e.location.lat),
        width: 180
      }
    ]
  })
}

export async function setupAirport() {
  const data = (await requestModel('Airport')) as Airport[]
  registerEntityType<Airport>({
    type: 'Airport',
    typeName: '机场',
    data
  })
  setupAirportTable()
  initAirportIndex(data)
  initMap(data)
  initAirportMapItems()
}
